﻿using SharpErp.Data.Common.Entity;
using SharpErp.Data.Common.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharpErp.Data.Service
{
    public partial class SharpErpServiceImpl : ISharpErpService
    {
        public List<ProductionFirstClass> GetSysProductionFirstClass()
        {
            var list = GetAllKvItemClass<ProductionFirstClass>(KVEntity.KEY_PRODUCTION_FIRST_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList();
        }

        public void AddProductionFirstClass(ProductionFirstClass item)
        {
            var items = GetAllKvItemClass<ProductionFirstClass>(KVEntity.KEY_PRODUCTION_FIRST_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_FIRST_CLASS);
        }



        public void UpdateProductionFirstClass(ProductionFirstClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ProductionFirstClass>(KVEntity.KEY_PRODUCTION_FIRST_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    items[i].Code = item.Code;
                    break;
                }
                SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_FIRST_CLASS);
            }
        }

        public void DeleteProductionFirstClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ProductionFirstClass>(KVEntity.KEY_PRODUCTION_FIRST_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_FIRST_CLASS);
            }
        }

        public string GetProductionFirstClassCode(int id)
        {
            string maxCode = "0000";
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ProductionFirstClass>(KVEntity.KEY_PRODUCTION_FIRST_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        return items[i].Code;
                    }
                    if (items[i].Code.CompareTo(maxCode) > 0) maxCode = items[i].Code;
                }
            }
            return string.Format("{0:D4}", int.Parse(maxCode) + 1);
        }

        public List<ProductionSecondClass> GetSysProductionSecondClass()
        {
            var list = GetAllKvItemClass<ProductionSecondClass>(KVEntity.KEY_PRODUCTION_SECOND_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList();
        }

        public List<ProductionSecondClass> GetSysProductionSecondClassByParentId(int parentId)
        {
            var list = GetSysProductionSecondClass();
            if (list == null || !list.Any()) return list;
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
            ;
        }

        public void AddProductionSecondClass(ProductionSecondClass item)
        {
            var items = GetAllKvItemClass<ProductionSecondClass>(KVEntity.KEY_PRODUCTION_SECOND_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_SECOND_CLASS);
        }

        public void UpdateProductionSecondClass(ProductionSecondClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ProductionSecondClass>(KVEntity.KEY_PRODUCTION_SECOND_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    items[i].ManufactorName = item.ManufactorName;
                    break;
                }
                SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_SECOND_CLASS);
            }
        }

        public void DeleteProductionSecondClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ProductionSecondClass>(KVEntity.KEY_PRODUCTION_SECOND_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_SECOND_CLASS);
            }
        }

        public List<ProductionThirdClass> GetSysProductionThirdClassByParentId(int parentId)
        {
            var list = GetAllKvItemClass<ProductionThirdClass>(KVEntity.KEY_PRODUCTION_THIRD_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
        }

        public void AddProductionThirdClass(ProductionThirdClass item)
        {
            var items = GetAllKvItemClass<ProductionThirdClass>(KVEntity.KEY_PRODUCTION_THIRD_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_THIRD_CLASS);
        }

        public void UpdateProductionThirdClass(ProductionThirdClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ProductionThirdClass>(KVEntity.KEY_PRODUCTION_THIRD_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    items[i].Deduct = item.Deduct;
                    break;
                }
                SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_THIRD_CLASS);
            };
        }

        public void DeleteProductionThirdClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ProductionThirdClass>(KVEntity.KEY_PRODUCTION_THIRD_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_THIRD_CLASS);
            }
        }

        public List<ProductionForthClass> GetSysProductionFourClassByParentId(int parentId)
        {
            var list = GetAllKvItemClass<SharpErp.Data.Common.Entity.ProductionForthClass>(KVEntity.KEY_PRODUCTION_FORTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
        }

        public void AddProductionFourClass(ProductionForthClass item)
        {
            var items = GetAllKvItemClass<ProductionForthClass>(KVEntity.KEY_PRODUCTION_FORTH_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_FORTH_CLASS);
        }

        public void UpdateProductionFourClass(ProductionForthClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ProductionForthClass>(KVEntity.KEY_PRODUCTION_FORTH_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    break;
                }
                SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_FORTH_CLASS);
            };
        }

        public void DeleteProductionFurClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ProductionForthClass>(KVEntity.KEY_PRODUCTION_FORTH_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_FORTH_CLASS);
            }
        }

        public List<ProductionFifthClass> GetSysProductionFiveClassByParentId(int parentId)
        {
            var list = GetAllKvItemClass<SharpErp.Data.Common.Entity.ProductionFifthClass>(KVEntity.KEY_PRODUCTION_FIFTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
        }

        public void AddProductionFiveClass(ProductionFifthClass item)
        {
            var items = GetAllKvItemClass<ProductionFifthClass>(KVEntity.KEY_PRODUCTION_FIFTH_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_FIFTH_CLASS);
        }

        public void UpdateProductionFiveClass(ProductionFifthClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ProductionFifthClass>(KVEntity.KEY_PRODUCTION_FIFTH_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    break;
                }
                SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_FIFTH_CLASS);
            };
        }

        public void DeleteProductionFiveClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ProductionFifthClass>(KVEntity.KEY_PRODUCTION_FIFTH_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_FIFTH_CLASS);
            }
        }

        public List<ProductionSixClass> GetSysProductionSixClassByParentId(int parentId)
        {
            var list = GetAllKvItemClass<SharpErp.Data.Common.Entity.ProductionSixClass>(KVEntity.KEY_PRODUCTION_SIXTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
        }

        public void AddProductionSixClass(ProductionSixClass item)
        {
            var items = GetAllKvItemClass<ProductionSixClass>(KVEntity.KEY_PRODUCTION_SIXTH_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_SIXTH_CLASS);
        }

        public void UpdateProductionSixClass(ProductionSixClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ProductionSixClass>(KVEntity.KEY_PRODUCTION_SIXTH_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    break;
                }
                SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_SIXTH_CLASS);
            };
        }

        public void DeleteProductionSixClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ProductionSixClass>(KVEntity.KEY_PRODUCTION_SIXTH_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass(items, KVEntity.KEY_PRODUCTION_SIXTH_CLASS);
            }
        }

        public List<ProductionSevenClass> GetSysProductionSevenClassByParentId(int parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.ProductionSevenClasses,
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && p.ParentId == parentId);
            }
        }


        public void AddProductionSevenClass(ProductionSevenClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Add(context.ProductionSevenClasses, item);
            }
        }

        public void UpdateProductionSevenClass(ProductionSevenClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Update(context.ProductionSevenClasses, item);
            }
        }

        public void DeleteProductionSevenClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Delete(context.ProductionSevenClasses, p => p.Id == id);
            }
        }


        public List<ProductionEightClass> GetSysProductionEightClassByParentId(int parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.ProductionEightClasses,
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && p.ParentId == parentId);
            }
        }

        public void AddProductionEightClass(ProductionEightClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Add(context.ProductionEightClasses, item);
            }
        }

        public void UpdateProductionEightClass(ProductionEightClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Update(context.ProductionEightClasses, item);
            }
        }

        public void DeleteProductionEightClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Delete(context.ProductionEightClasses, p => p.Id == id);
            }
        }

        public List<ProductionTenClass> GetSysProductionTenClassByParentId(int parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.ProductionTenClasses,
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && p.ParentId == parentId);
            }
        }

        public void AddProductionTenClass(ProductionTenClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Add(context.ProductionTenClasses, item);
            }
        }

        public void UpdateProductionTenClass(ProductionTenClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Update(context.ProductionTenClasses, item);
            }
        }


        public void DeleteProductionTenClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Delete(context.ProductionTenClasses, p => p.Id == id);
            }
        }


        public List<ProductionNineClass> GetSysProductionNineClassByParentId(int parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.ProductionNineClasses,
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && p.ParentId == parentId);
            }
        }

        public List<ProductionNineClass> GetSysProductionNineClassByParentIds(HashSet<int> parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.ProductionNineClasses,
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId));
            };
        }

        public int AddProductionNineClass(ProductionNineClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.Add(context.ProductionNineClasses, item).Id;
            }
        }

        public int AddProductionNineClassWithBindGoods(ProductionNineClass item, List<CustomerGoodsClass> goodsList)
        {
            if (goodsList == null || goodsList.Count <= 0) return AddProductionNineClass(item);
            int id = 0;
            using (var context = new Common.Context.SharpErpDbContext())
            {
                foreach (var goodsItem in goodsList)
                {
                    var goodsItemNineClass = context.SelectOne(context.ItemNineClasses, p => p.Id == goodsItem.NineClassId);
                    if(goodsItemNineClass != null)
                    {
                        var goodsItemEightClass = context.SelectOne(context.ItemEightClasses, p => p.Id == goodsItemNineClass.ParentId);
                        if(goodsItemEightClass != null)
                        {
                            item.GoodsDesc = goodsItemEightClass.ParentDesc;
                            item.GoodsDesc += (goodsItemEightClass.Standard) + SharpErp.Data.Util.Constants.NAME_SEPERATOR;
                            item.GoodsDesc += (goodsItemEightClass.Length) + SharpErp.Data.Util.Constants.NAME_SEPERATOR;
                            item.GoodsDesc += (goodsItemEightClass.Width) + SharpErp.Data.Util.Constants.NAME_SEPERATOR;
                            item.GoodsDesc += (goodsItemEightClass.Pcount) + SharpErp.Data.Util.Constants.NAME_SEPERATOR;
                            item.GoodsDesc += (goodsItemEightClass.Unit) + SharpErp.Data.Util.Constants.NAME_SEPERATOR;
                            item.GoodsDesc += (goodsItemEightClass.Manufactor) + SharpErp.Data.Util.Constants.NAME_SEPERATOR;
                            item.GoodsDesc += (goodsItemEightClass.Facility) + SharpErp.Data.Util.Constants.NAME_SEPERATOR;
                            item.Goods = item.GoodsDesc.Split(new string[] { Util.Constants.NAME_SEPERATOR }, StringSplitOptions.None)[1];
                        }
                    }
                    id = context.Add(context.ProductionNineClasses, item).Id;
                    item.Id = id;
                    var secondClass = SharpErp.Data.Common.Business.CustomerSecondClassConverter.convertToCustomerSecondClass(item);
                    var temp = new ProductionNineClass();
                    SharpErp.Common.Util.BeanUtils.CopyTo(item, temp);
                    temp.Id = 0;
                    item = temp;
                    goodsItem.CustomerFirstId = secondClass.ParentId;
                    goodsItem.CustomerSecondId = secondClass.Id;
                    context.Add(context.CustomerGoodsClasses, goodsItem);
                }
            }
            return id;

        }

        public void UpdateProductionNineClass(ProductionNineClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Update(context.ProductionNineClasses, item);
            }
        }

        public void UpdateProductionNineClassWithBindGoods(ProductionNineClass item, List<CustomerGoodsClass> goodsList)
        {
            if (goodsList == null || goodsList.Count <= 0) UpdateProductionNineClass(item);
            using (var context = new Common.Context.SharpErpDbContext())
            {
                for (int i = 0; i < goodsList.Count(); ++i )
                {
                    var goodsItem = goodsList[i];
                    var goodsItemNineClass = context.SelectOne(context.ItemNineClasses, p => p.Id == goodsItem.NineClassId);
                    if(goodsItemNineClass != null)
                    {
                        var goodsItemEightClass = context.SelectOne(context.ItemEightClasses, p => p.Id == goodsItemNineClass.ParentId);
                        if(goodsItemEightClass != null)
                        {
                            item.GoodsDesc = goodsItemEightClass.ParentDesc;
                            item.GoodsDesc += (goodsItemEightClass.Standard) + SharpErp.Data.Util.Constants.NAME_SEPERATOR;
                            item.GoodsDesc += (goodsItemEightClass.Length) + SharpErp.Data.Util.Constants.NAME_SEPERATOR;
                            item.GoodsDesc += (goodsItemEightClass.Width) + SharpErp.Data.Util.Constants.NAME_SEPERATOR;
                            item.GoodsDesc += (goodsItemEightClass.Pcount) + SharpErp.Data.Util.Constants.NAME_SEPERATOR;
                            item.GoodsDesc += (goodsItemEightClass.Unit) + SharpErp.Data.Util.Constants.NAME_SEPERATOR;
                            item.GoodsDesc += (goodsItemEightClass.Manufactor) + SharpErp.Data.Util.Constants.NAME_SEPERATOR;
                            item.GoodsDesc += (goodsItemEightClass.Facility) + SharpErp.Data.Util.Constants.NAME_SEPERATOR;
                            item.Goods = item.GoodsDesc.Split(new string[] { Util.Constants.NAME_SEPERATOR }, StringSplitOptions.None)[1];
                        }
                    }
                    if (i == 0) context.Update(context.ProductionNineClasses, item);
                    else item.Id = context.Add(context.ProductionNineClasses, item).Id;
                    var secondClass = SharpErp.Data.Common.Business.CustomerSecondClassConverter.convertToCustomerSecondClass(item);
                    var temp = new ProductionNineClass();
                    SharpErp.Common.Util.BeanUtils.CopyTo(item, temp);
                    temp.Id = 0;
                    item = temp;
                    goodsItem.CustomerFirstId = secondClass.ParentId;
                    goodsItem.CustomerSecondId = secondClass.Id;
                    context.Add(context.CustomerGoodsClasses, goodsItem);
                }
            }
        }

        public void DeleteProductionNineClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Delete(context.ProductionNineClasses, p => p.Id == id);
            }
        }

        public List<ProductionSecondClass> GetSysProductionSecondClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvItemClass<ProductionSecondClass>(KVEntity.KEY_PRODUCTION_SECOND_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<ProductionThirdClass> GetSysProductionThirdClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvItemClass<ProductionThirdClass>(KVEntity.KEY_PRODUCTION_THIRD_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<ProductionForthClass> GetSysProductionFourClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvItemClass<SharpErp.Data.Common.Entity.ProductionForthClass>(KVEntity.KEY_PRODUCTION_FORTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<ProductionFifthClass> GetSysProductionFiveClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvItemClass<SharpErp.Data.Common.Entity.ProductionFifthClass>(KVEntity.KEY_PRODUCTION_FIFTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<ProductionSixClass> GetSysProductionSixClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvItemClass<SharpErp.Data.Common.Entity.ProductionSixClass>(KVEntity.KEY_PRODUCTION_SIXTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<ProductionSevenClass> GetSysProductionSevenClassByParentIds(HashSet<int> parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.ProductionSevenClasses,
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId));
            };
        }

        public List<ProductionEightClass> GetSysProductionEightClassByParentIds(HashSet<int> parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.ProductionEightClasses,
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId));
            };
        }

        public List<ProductionTenClass> GetSysProductionTenClassByParentIds(HashSet<int> parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.ProductionTenClasses,
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId));
            };
        }

        public string GetProductionEightClassCode(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                if (id > 0)
                {
                    var item = context.SelectOne(context.ProductionEightClasses, p => p.Id == id);
                    if (item != null)
                    {
                        return item.Code;
                    }
                }

                var maxId = context.ProductionEightClasses.Max(p => p.Code != null ? p.Code.Substring(p.Code.Length - 7) : p.Code); ;
                if (string.IsNullOrEmpty(maxId))
                {
                    return "S0000001";
                }
                else
                {
                    var number = maxId.Substring(maxId.Length - 7);
                    return string.Format("S{0:D7}", int.Parse(number) + 1);
                }

            }
        }

        public void CloneProductionSevenClass(HashSet<int> ids, bool includeChildren)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var sevenClassList = context.SelectAll(context.ProductionEightClasses, p => ids.Contains(p.Id));
                foreach (var itemSevenClass in sevenClassList)
                {
                    var Id = itemSevenClass.Id;
                    itemSevenClass.Id = 0;
                    var newProductionSevenClass = context.Add(context.ProductionEightClasses, itemSevenClass);
                    if (includeChildren)
                    {
                        var eightClassList = context.SelectAll(context.ProductionTenClasses, p => p.ParentId == Id);
                        foreach (var itemEightClass in eightClassList)
                        {
                            itemEightClass.Id = 0;
                            itemEightClass.ParentId = newProductionSevenClass.Id;
                            context.Add(context.ProductionTenClasses, itemEightClass);
                        }
                    }
                    context.SaveChanges();
                }
            }
        }

        public void CloneProductionEightClass(HashSet<int> ids)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var eightClassList = context.SelectAll(context.ProductionTenClasses, p => ids.Contains(p.Id));
                foreach (var itemEightClass in eightClassList)
                {
                    itemEightClass.Id = 0;
                    context.Add(context.ProductionTenClasses, itemEightClass);
                }
                context.SaveChanges();

            }
        }

        private QueryGoodsInfoView getQueryGoodsInfoView(ProductionEightClass eightClass, ProductionTenClass nineClass)
        {
            return new QueryGoodsInfoView()
            {
                Name = eightClass.Name,
                Code = eightClass.Code,
                Length = eightClass.Length + "*" + eightClass.Width + "*" + eightClass.ParentDesc?.Split(new string[] { "$$" }, StringSplitOptions.RemoveEmptyEntries)[3],
                Facility = eightClass.Facility,
                Standard = eightClass.Standard,
                Unit = eightClass.Unit,
                ParentDesc = eightClass.ParentDesc,
                Area = nineClass.Area,
                AreaPrice = nineClass.AreaPrice,
                BuyPrice = nineClass.BuyPrice,
                CheapPrice = nineClass.CheapPrice,
                Inventory = nineClass.Inventory,
                MemberPrice = nineClass.MemberPrice,
                Pcount = eightClass.Pcount,
                RealMaterialPrice = nineClass.RealMaterialPrice,
                Requirement = eightClass.Requirement,
                Width = eightClass.Width,
                SalePrice = nineClass.SalePrice,
                ParentId = eightClass.ParentId,
                EightClassId = eightClass.Id,
                NineClassId = nineClass.Id,
            };

        }

    }

}
